package com.lsy.weizhi2.picture;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.mybase.BaseApp;
import com.lsy.weizhi2.myutils.SendPhotoCall;
import com.lsy.weizhi2.utils.BitmapUtils;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;
import com.zhy.autolayout.utils.AutoUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by 嗨哟 on 2017/1/12.
 */
public class PicturePopup implements View.OnClickListener{
    private Context context;
    private PopupWindow popupWindow;
    private SendPhotoCall sendPhotoCall;
    public void init(Context context,SendPhotoCall sendPhotoCall){
        this.context=context;
        this.sendPhotoCall=sendPhotoCall;
        int height= BaseApp.REAL_HEIGHT*311/1334;
        View view = View.inflate(context, R.layout.picture_window,null);
        popupWindow=new PopupWindow(view,BaseApp.SCREEN_WIDTH, height,true);
        TextView photograph= (TextView) view.findViewById(R.id.photograph);
        TextView album= (TextView) view.findViewById(R.id.album);
        TextView cancel= (TextView) view.findViewById(R.id.cancel);
        photograph.setOnClickListener(this);
        album.setOnClickListener(this);
        cancel.setOnClickListener(this);
        popupWindow.setAnimationStyle(R.style.pictrue_popup);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        popupWindow.setBackgroundDrawable(dw);
        popupWindow.showAsDropDown(view,0,BaseApp.REAL_HEIGHT-height);
        hideWindow();
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                showWindow();
            }
        });
    }
    private void hideWindow(){
        WindowManager.LayoutParams lp = ((Activity)context).getWindow().getAttributes();
        lp.alpha = 0.5f;
        ((Activity)context).getWindow().setAttributes(lp);
    }
    private void showWindow(){
        WindowManager.LayoutParams lp = ((Activity)context).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity)context).getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.album:
                PictureUtils.getPictureFromAlbum((Activity) context, new IWxCallback() {
                    @Override
                    public void onSuccess(Object... var1) {
                        String path = (String) var1[0];
                        Log.i("PicturePopup","返回的path=="+path);
                        File file = new File(path);
                        long length=file.length();
                        Log.i("PicturePopup","length=="+length);
                        if(length>=1048576){
                            Bitmap bitmap=BitmapUtils.getimage(path);
                            String path1=file.getParent();
                            file = new File(path1,"/temp.jpg");
                            FileOutputStream fos = null;
                            try {
                                fos = new FileOutputStream(file);
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }finally {
                                if(fos!=null){
                                    try {
                                        fos.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                            bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
                        }
                        long length1=file.length();
                        Log.i("PicturePopup","length1=="+length1);
                        MyOkHttpUtils.getInstance(context).upLoadImage(Constants.UP_USER_PICTURE,file).excute(new MyOkHttpUtils.OKCallBack() {
                            @Override
                            public void onFailure(String message) {
                                Log.i("PicturePopup","F=="+message);
                                MyMethod.ShowDialog(context,"图片发送失败");
                            }
                            @Override
                            public void onResponse(String message) {
                                Log.i("PicturePopup","S=="+message);
                                try {
                                    JSONObject jsonObject=new JSONObject(message);
                                    boolean result=jsonObject.getBoolean("result");
                                    if(result){
                                        sendPhotoCall.Request(message);
                                    }else{
                                        String msg=jsonObject.getString("message");
                                        MyMethod.ShowDialog(context,msg);
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });

                    }
                    @Override
                    public void onError(int var1, String var2) {

                    }

                    @Override
                    public void onProgress(int var1) {

                    }
                });
                popupWindow.dismiss();
                break;
            case R.id.photograph:
                PictureUtils.PreviewImage((Activity) context);
                popupWindow.dismiss();
                break;
            case R.id.cancel:
                popupWindow.dismiss();
                break;
        }
    }




}
